package utilities;



import java.io.BufferedWriter;
import java.util.*;
import java.lang.reflect.*;

public class ProgramProcessor extends Processor {


	public ProgramProcessor()
	{
	}
	
	public ProgramProcessor(String name, BufferedWriter log, List actionList,
			Object configData, int sleeps) {		
		super(name, log, actionList, configData, sleeps);
	}

	public void run()
	{
		try
		{
        String key;
		active = true;
		int actSize = 0;
		while(running)
			{
				if(active)
				{
					//get all actions available, process each one
					actSize =workQueue.size();
					for(int i=0; i< actSize;i++)
					{
						//process each action
						 key = workQueue.get(i).toString();						 
						 //log.write("Processing key: " + key + "\n");
				  	    	// this.queueCommand(inputLine);	        	
				  	    	if(key.indexOf("Pause")>-1)
				  	    	{
				  	    		//threadsleep the diff
				  	    		int qMark = key.indexOf("?");				  	    		
				  	    		String val = key.substring((qMark+1),
				  	    				(key.length()));
				  	    		int sleep = Integer.valueOf(val).intValue();
						        System.out.println("sleeping " + sleep);
				  	    		Thread.sleep(sleep);
				  	    	}
				  	    	else
				  	    	{
						        System.out.println("Delegating " + key);
					  	    	servers.Dragon.commandDelegator(key);
				  	    	}
				  	    }							 												 
					}
					for(int i=0; i< actSize;i++)
					{
						//remove action
						workQueue.remove(0);
					}					
				}	
			this.sleep(sleeps);
						
		}
		catch(Exception e)
		{
			try
			{
			log.write("Thread died: " + name + "\n" + e.getMessage());
			}catch(Exception ee) {}
		}
	}

	
	
	
	
	
}
